//
//  LYSupportTools.m
//  BaChat
//
//  Created by 刘洋 on 16/6/21.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "LYSupportTools.h"

@implementation LYSupportTools


+(void)LYMBHUDCustomViewForExclamationPointAddToView:(UIView *)view{

     MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
     
     hud.mode = MBProgressHUDModeCustomView;
     UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gantanhao_"]];
     image.frame = CGRectMake(0, 0, 50, 50);
     image.backgroundColor = [UIColor clearColor];
     hud.customView = image;
    [image release];
    
     hud.labelText = @"说话时间太短";
     [hud hide:YES afterDelay:0.8];


    
}


+(void)LYMBHUDAnimationForRecordingAddToView:(UIView *)view{
    
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.mode = MBProgressHUDModeCustomView;
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 0, 100, 100);
    imageView.backgroundColor = [UIColor clearColor];
    hud.customView = imageView;
    
    imageView.animationImages = @[
                                  [UIImage imageNamed:@"voice_volume_1_blue"],
                                  [UIImage imageNamed:@"voice_volume_2_blue"],
                                  [UIImage imageNamed:@"voice_volume_3_blue"],
                                  [UIImage imageNamed:@"voice_volume_4_blue"],
                                  [UIImage imageNamed:@"voice_volume_5_blue"],
                                  [UIImage imageNamed:@"voice_volume_6_blue"],
                                  ];
    
    imageView.animationDuration = 1;
    [imageView startAnimating];
    
    [imageView release];
    
    hud.labelText = @"正在录音";
  
    
    
    
    
    
}



/*
 1.将text字符串转换为可变的富文本NSMutableAttributedString
 2.通过正则表达式统计出text内容中所有表情符号的位置,并存入数组(数组保存着正则对象NSTextCheckingResult,该对象属性range可得到位置)
 3.遍历数组,根据位置获取到text内容中的表情符号
 4.根据表情符号获得对应的image名字
 5.创建一个字典,将image名字和range存入字典中,将字典存入数组中
 6.遍历数组,根据range位置将富文本中的表情符号替换为图片
 
 */

+(NSMutableAttributedString *)emotionStrWithString:(NSString *)text{
 
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"EmoticonsList" ofType:@"plist"];
    
 //   NSLog(@"%@",filePath);
    
    
    NSArray *face = [NSArray arrayWithContentsOfFile:filePath];
    
  //  NSLog(@"%@",face);
    
    //1.创建一个可变的属性字符串
    NSMutableAttributedString *attributesString = [[NSMutableAttributedString alloc]initWithString:text];
    //2.通过正则表达式来匹配字符串
    NSString *regex_emoji = @"\\[[a-zA-Z0-9\\/\\u4e00-\\u9fa5]+\\]"; //匹配表情
    NSError *error = nil;
    //筛选出符合正则表达式满足的字符
    NSRegularExpression *result = [NSRegularExpression regularExpressionWithPattern:regex_emoji options:NSRegularExpressionCaseInsensitive error:&error];
    
    if (!result) {
        NSLog(@"%@",[error localizedDescription]);
        return attributesString;
    }
    //筛选出符合正则表达式满足的字符,并以数组形式存储,数组中存储着表情位置
    NSArray *resultArray = [result matchesInString:text options:0 range:NSMakeRange(0, text.length)];
    
    NSMutableArray *imageArray = [NSMutableArray arrayWithCapacity:resultArray.count];
    
    //根据匹配范围来用图片进行相应的替换
    for (NSTextCheckingResult *match in resultArray) {
        //获取数组元素中得到的range
        NSRange range = [match range];
        //获取原字符串中对应的值
        NSString *subStr = [text substringWithRange:range];
        
        for (int i = 0; i < face.count; i++) {
            if ([face[i][@"cht"] isEqualToString:subStr]) {
                //face[i][@"png"]就是我们要加载的图片
                //新建文字附件来存放我们的图片,iOS7才新加的对象
                NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];
                //给附件添加图片
                textAttachment.image = [UIImage imageNamed:face[i][@"png"]];
           
                
                //调整一下图片的位置,如果你的图片偏上或者偏下,调整一下boundsy值即可
                textAttachment.bounds = CGRectMake(0, -5, 20, 20);
                //把附件转换成可变字符串,用于替换掉原字符串中的表情文字
                NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
            //    [textAttachment release];
                //把图片和图片对应的位置存入字典中
                NSMutableDictionary *imageDic = [NSMutableDictionary dictionary];
                [imageDic setObject:imageStr forKey:@"image"];
                [imageDic setObject:[NSValue valueWithRange:range] forKey:@"range"];
                //把字典存入数组中
                [imageArray addObject:imageDic];
            }
        }
    }
    
  //4.从后往前替换,否则会引起位置问题
    for (int i = (int)imageArray.count - 1; i >= 0; i--) {
        NSRange range;
        [imageArray[i][@"range"] getValue:&range];
        //进行替换
        [attributesString replaceCharactersInRange:range withAttributedString:imageArray[i][@"image"]];
    }
    return attributesString;
}



+(NSString *)messageTimeWithTimestamp:(long long)timestamp{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDate *currentDate = [NSDate date];
    
    // 获取当前时间的年、月、日
    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger currentYear = components.year;
    NSInteger currentMonth = components.month;
    NSInteger currentDay = components.day;
    
    // 获取消息发送时间的年、月、日
    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:timestamp/1000.0];
    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay fromDate:msgDate];
    CGFloat msgYear = components.year;
    CGFloat msgMonth = components.month;
    CGFloat msgDay = components.day;
    
    // 判断
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {
        //今天
        dateFmt.dateFormat = @"HH:mm";
    }else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-1 == msgDay ){
        //昨天
        dateFmt.dateFormat = @"昨天 HH:mm";
    }else{
        //昨天以前
        dateFmt.dateFormat = @"yyyy-MM-dd HH:mm";
    }
    
    NSString *timeString = [dateFmt stringFromDate:msgDate];
    
    NSLog(@"%@",timeString);
    

    
    return timeString;
    
    
    
    
}












@end
